﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace OpenSoftStore.Client.UIL.UserControls
{
    internal class YhSoftDataGridView : DataGridView
    {
        public DataGridViewTextBoxColumn ColSystemId;
        public DataGridViewTextBoxColumn SystemId;

        public YhSoftDataGridView()
        {

            this.RowHeadersWidth = 28;
            this.AllowUserToResizeColumns = false;
            this.AllowUserToResizeRows = false;
            this.BackgroundColor = System.Drawing.Color.White;
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
            this.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            this.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.SystemColors.Control;
            this.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ColumnHeadersDefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102)))));
            this.ColumnHeadersHeight = 30;
            this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //this.Dock = System.Windows.Forms.DockStyle.Fill;
            this.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
            this.GridColor = System.Drawing.Color.Silver;
            this.Location = new System.Drawing.Point(0, 0);
            this.MultiSelect = false;
            this.RowHeadersVisible = false;
            this.RowTemplate.DefaultCellStyle.BackColor = System.Drawing.Color.White;
            this.RowTemplate.DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102)))));
            this.RowTemplate.DefaultCellStyle.Padding = new System.Windows.Forms.Padding(2, 0, 2, 0);
            this.RowTemplate.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            this.RowTemplate.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102)))));
            this.RowTemplate.DefaultCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.RowTemplate.Height = 30;
            this.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False;
            this.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.Size = new System.Drawing.Size(100, 100);

            this.AutoGenerateColumns = false;
            this.EnableHeadersVisualStyles = false;
            this.AllowDrop = true;
            this.AllowUserToOrderColumns = true;
            this.AllowUserToResizeColumns = true;
            this.AllowUserToResizeRows = true;
            this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            this.ColumnHeadersHeight = 45;
            this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(242, 242, 242);
            this.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.FromArgb(70, 141, 75);
            //dgv.RowTemplate.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(153, 153, 153);
            this.RowTemplate.DefaultCellStyle.SelectionForeColor = Color.FromArgb(255, 255, 255);
            this.RowTemplate.Height = 40;

            this.RowPostPaint += DgvGrid_RowPostPaint;
            this.DataError += DgvGrid_DataError;
            this.CellPainting += DgvGrid_CellPainting;
            DgvGrid_Paint(this);
            //string[] SummaryColumns=new string[3];
            //ShowSummary(SummaryColumns);
        }
        public void SetDataGridStyle(DataGridView dgv, int columnHeadersHeight = 45, int rowTemplateHeight = 40)
        {
         
        }

        public void DgvGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            this.ColumnHeadersHeight = 45;
        }

        public void DgvGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var dgv = sender as DataGridView;
            dgv.RowHeadersVisible = true; 
            if (dgv == null) return;

            if (e.RowIndex%2==0)
            {
                dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(248, 248, 248);
            }
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
             e.RowBounds.Location.Y,
             dgv.RowHeadersWidth - 4,
             e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dgv.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dgv.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            dgv.RowHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(70, 141, 75);
        }

        public void DgvGrid_DataError(object sender, DataGridViewDataErrorEventArgs e) { }

        public void DgvGrid_Paint(DataGridView dgv)
        {
            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                var item = dgv.Columns[i];
                var valueType = "";
                if (item.ValueType != null)
                    valueType = dgv.Columns[i].ValueType.ToString().ToLower();

                if (item.Width <= 120)
                {
                    if (item.HeaderText.Length <= 2)
                        item.Width = 60;
                    else if (item.HeaderText.Length >= 3 && item.HeaderText.Length <= 4)
                        item.Width = 80;
                    else
                        item.Width = 100;
                }
                switch (valueType)
                {
                    case "system.string":
                        continue;
                    case "system.decimal":

                        if (item.HeaderText.Contains("价") || item.HeaderText.Contains("额") || item.HeaderText.Contains("收") || item.HeaderText.Contains("款"))
                            item.DefaultCellStyle.Format = "C2";
                        else
                            item.DefaultCellStyle.Format = "N2";
                        continue;
                    case "system.int32":
                        if (item.HeaderText.Contains("价") || item.HeaderText.Contains("额") || item.HeaderText.Contains("款"))
                            item.DefaultCellStyle.Format = "C0";
                        else
                            item.DefaultCellStyle.Format = "N0";
                        continue;
                    case "system.datetime":
                        if (item.HeaderText.Contains("时间"))
                            item.Width = 110;
                        else
                            item.Width = 80;
                        continue;
                    default:
                        continue;
                }

            }


        }

        /// <summary>
        /// 显示DataGridView的统计信息
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="SummaryColumns">要统计的列名称或数据源绑定列名称</param>
        public void ShowSummary(string[] SummaryColumns)
        {
            SummaryControlContainer summaryControl = new SummaryControlContainer(this, SummaryColumns);
            this.Controls.Add(summaryControl);
            //dgv.Tag = summaryControl;
            summaryControl.BringToFront();
            summaryControl.Show();
        }
        /// <summary>
        /// 显示DataGridView的统计信息
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="DisplaySumRowHeader">是否显示合计行标题</param>
        /// <param name="SumRowHeaderText">合计列标题</param>
        /// <param name="SumRowHeaderTextBold">合计列标题用粗体显示</param>
        /// <param name="SummaryColumns">要统计的列名称或数据源绑定列名称</param>
        public void ShowSummary( bool DisplaySumRowHeader, string SumRowHeaderText, bool SumRowHeaderTextBold, string[] SummaryColumns)
        {
            SummaryControlContainer summaryControl = new SummaryControlContainer(this, DisplaySumRowHeader, SumRowHeaderText, SumRowHeaderTextBold, SummaryColumns);
            this.Controls.Add(summaryControl);
            //dgv.Tag = summaryControl;
            summaryControl.BringToFront();
            summaryControl.Show();
        }


    }
}